using System;
using System.Data;
using System.Web.UI;
using PHSRAG.Insight21.Common.Pages;
using PHSRAG.Insight21.InsightWorkFlow;
using PHSRAG.Utility;
using HumansWF = PHSRAG.Insight21.InsightWorkFlow;

namespace PHSRAG.Insight21.Humans
{
    /// <summary>
    /// Summary description for WorkFlowReport.
    /// </summary>
    public partial class WorkFlowReport : BasePage
    {
        #region Private Constants

        private const string PPIdWorkflowReport = "PPIdWorkflowReport_DC46908B-235B-4fc6-A234-4E7AED4AB3E2";
        private const string WFApp = "EIRBWFApp";

        #endregion

        #region Private Variables

        private int _executionId;
        private int _protocolProcessId;

        #endregion

        #region Events

        /// <summary>
        /// Loads the page. Sets Workflow report object's properties			
        /// </summary>
        /// <param name="sender">Sender of the event</param>
        /// <param name="e">Argument associated with the event</param>
        protected void OnPageLoad(object sender, EventArgs e)
        {
            try
            {
                string ppId = Request.QueryString["PPId"];
                string execId = Request.QueryString["ExecId"];

                _protocolProcessId = Convert.ToInt32(ppId);

                if (execId != null)
                {
                    _executionId = Convert.ToInt32(execId);
                }
                if (! IsPostBack)
                {
                    LoadWorkflowHistory();
                }
            }

            catch (Exception ex)
            {
                ExceptionReport.WriteToEventLog("Failed to open Workflow report", ex, true, ExceptionReport.ReportType.Error);
                annunciator.Message = "Failed to open Workflow report";
            }
        }

        #endregion

        #region Web Form Designer generated code

        protected override void OnInit(EventArgs e)
        {
            //
            // CODEGEN: This call is required by the ASP.NET Web Form Designer.
            //
            InitializeComponent();
            base.OnInit(e);
        }

        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            this.Load += new System.EventHandler(this.OnPageLoad);
            view.Click += new ImageClickEventHandler(OnViewClick);
        }

        #endregion

        #region Private methods

        /// <summary>
        /// This method will load the available submissions for the process
        /// </summary>
        /// <param name="dtSubmission"></param>
        private void LoadSubmissions(DataTable dtSubmission)
        {
            if (null == dtSubmission || dtSubmission.Rows.Count == 0)
            {
                return;
            }
            submissionList.DataSource = dtSubmission;
            submissionList.DataBind();
            submissionList.Visible = view.Visible = true;
        }

        /// <summary>
        /// This private method will load the workflow history
        /// </summary>
        private void LoadWorkflowHistory()
        {
            HideControls();
            if (0 != _protocolProcessId)
            {
                DataSet wfHistory = HumansWorkFlow.GetWorkFlowHistory(_protocolProcessId, _executionId);
                if (wfHistory == null || wfHistory.Tables.Count == 0)
                {
                    HideControls();
                }
                else if (wfHistory.Tables.Count > 0)
                {
                    annunciator.Message = "WorkFlow History";
                    if (wfHistory.Tables.Contains("WFSubmission") && !IsPostBack && wfHistory.Tables["WFSubmission"].Rows.Count > 0)
                    {
                        instruction.Visible = true;
                        LoadSubmissions(wfHistory.Tables["WFSubmission"]);
                    }

                    if (wfHistory.Tables.Contains("WFHistory") && wfHistory.Tables["WFHistory"].Rows.Count > 0)
                    {
                        workFlowInitialSubmission.PageSize = wfHistory.Tables["WFHistory"].Rows.Count;
                        workFlowInitialSubmission.DataSource = wfHistory.Tables["WFHistory"];
                        workFlowInitialSubmission.DataBind();
                        workFlowInitialSubmission.Visible = initialWFDetailsCaption.Visible = true;
                    }
                    else
                    {
                        annunciator.Message = "No workflow history available";
                        workFlowInitialSubmission.Visible = initialWFDetailsCaption.Visible = false;
                    }

                    if (wfHistory.Tables.Contains("WFSubmitter") && wfHistory.Tables["WFSubmitter"].Rows.Count > 0)
                    {
                        submittedBy.Text = string.Format("Submitted By: {0}", wfHistory.Tables["WFSubmitter"].Rows[0].ItemArray[0]);
                        submittedBy.Visible = true;
                    }
                    initialWFDetailsCaption.Text = (submissionList.Visible) ? submissionList.SelectedItem.Text + " workflow history" : string.Empty;
                }
            }
        }

        /// <summary>
        /// This method will hide the controls that doesn't have a source to be populated
        /// </summary>
        private void HideControls()
        {
            annunciator.Message = "No workflow history available";
            workFlowInitialSubmission.Visible = false;
        }

        #endregion

        #region EventHandler

        protected void OnViewClick(object sender, EventArgs e)
        {
            _executionId = Convert.ToInt32(submissionList.SelectedItem.Value);
            LoadWorkflowHistory();
        }

        #endregion
    }
}